#include <func.h>

int removeElement(int* nums, int numsSize, int val)
{
    if(numsSize == 1)
    {
        if(nums[0] == val)
            return 0;
        else
            return 1;
    }
    int left = 0, right = numsSize - 1;
    while(left < right)
    {
        while(left < right && nums[left] != val) left++;
        //没找到值--left ==right
        if(left == right && right == numsSize - 1)
        {
            if(nums[left + 1] == val)
                return left;
            else
                return left + 1;
        }
        if(left == right && right != numsSize - 1)
            return left;
        while(left < right && nums[right] == val) right--;
        if(left == right)
            return right;
        //交换元素
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }
    return left;
}
